﻿Imports System.Data.SqlClient

Public Class FrmPeliculaWithDataBindingFilter
    'Podemos utilizar las propiedades Filter y Sort del objeto BindingSource para manejar
    'el subconjunto de registros con el cual mantenemos enlazados nuestros controles a los
    'datos que éstos contienen.
    Private Sub PeliculaBindingNavigatorSaveItem_Click(sender As System.Object, e As System.EventArgs) Handles PeliculaBindingNavigatorSaveItem.Click
        If Me.Validate() Then
            Try
                Me.PeliculaBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.VideoDataSet)
            Catch ex As DBConcurrencyException
                MessageBox.Show("Error de concurrencia. En este caso se vuelven a actualizar todos los datos originales")
                PeliculaTableAdapter.Fill(VideoDataSet.Pelicula)
            Catch ex As DataException
                MessageBox.Show("Error en alguna restricción de la base de datos")
                PeliculaBindingSource.CancelEdit()
            Catch ex As SqlException
                MessageBox.Show("Error en la base de datos")
            End Try
        End If
    End Sub

    Private Sub FrmPelicula_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            GeneroToolStripCombo.ComboBox.DataSource = GeneroBindingSourceFilter
            GeneroToolStripCombo.ComboBox.DisplayMember = "nombre"
            GeneroToolStripCombo.ComboBox.ValueMember = "id"
            Me.PeliculaTableAdapter.Fill(Me.VideoDataSet.Pelicula)
            Me.GeneroTableAdapter.Fill(Me.VideoDataSet.Genero)
            FillPeliculasByGenero()
        Catch ex As SqlException
            MessageBox.Show("Error en la base de datos")
        End Try
    End Sub

    Private Sub FillPeliculasByGenero()
        Me.PeliculaBindingSource.Filter = "genero_id = " & CInt(GeneroToolStripCombo.ComboBox.SelectedValue)
    End Sub

    Private Sub GeneroToolStripCombo_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles GeneroToolStripCombo.SelectedIndexChanged
        PeliculaBindingSource.EndEdit()
        FillPeliculasByGenero()
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(sender As System.Object, e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        If MessageBox.Show("¿Está seguro que desea eliminar este elemento?", "Eliminar elemento", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            PeliculaBindingSource.RemoveCurrent()
        End If
    End Sub
End Class